package com.yuejie.resource;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yuejie.bean.ResultPojo;
import com.yuejie.bean.SongPojo;

public class Resource implements Iterator<SongPojo> {
	private static final Logger dblogger = LoggerFactory.getLogger("dbLogger");
	private Properties dbproperties;
	private Connection con;
	private String query = "select documentID, song_name,singer_name from song_history where flag =0 and song_name!=\"\" and song_name!=\"佚名\";";
	// private String insert = "insert into song_lyric values(NULL, ?, ?)";
	private String update = "update song_history set lyric = ?, flag=? where documentID=?";
	private Statement stmt_r;
	private PreparedStatement stmt_w;
	private ResultSet rs;

	public static void main(String[] arg) {
		SongPojo s;
		Resource r = new Resource();
		for (int i = 0; i < 10; i++) {
			s = r.next();
			System.out.println(s.getId() + "|" + s.getSong_name() + "|" + s.getSinger_name());
		}
		// ResultPojo rs = new ResultPojo();
		// rs.setCode(500);
		// r.write("1471635", rs);
		// r.destory();
	}

	public Resource() {
		java.io.InputStream ins = this.getClass().getResourceAsStream("/db.properties");
		dbproperties = new Properties();

		try {
			dbproperties.load(ins);
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(dbproperties.getProperty("url"),
					dbproperties.getProperty("user"), dbproperties.getProperty("password"));
			stmt_r = con.createStatement();
			stmt_w = con.prepareStatement(update);
			// stmt.setFetchSize(100);
			rs = stmt_r.executeQuery(query);

		} catch (SQLException ex) {
			dblogger.error("error when get the connetction");
			// System.err.print("SQLException: ");
			// System.err.println(ex.getMessage());
		} catch (java.lang.ClassNotFoundException e) {
			dblogger.error("error when find the com.mysql.jdbc.Driver");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 获取下一条歌曲信息
	 * 
	 * @return
	 */
	@Override
	public synchronized SongPojo next() {
		SongPojo s = null;
		try {
			rs.next();
			s = new SongPojo();
			s.setId(rs.getString(1));
			s.setSong_name(rs.getString(2));
			s.setSinger_name(rs.getString(3));
		} catch (SQLException e) {
			dblogger.error("error when get the next songPojo");
			e.printStackTrace();
		}
		return s;
	}

	public void destory() {
		try {
			stmt_r.close();
			stmt_w.close();
			con.close();
		} catch (SQLException e) {
			dblogger.error("error when destory the connetction");
			// e.printStackTrace();
		}
	}

	@Override
	public synchronized boolean hasNext() {
		boolean b = false;
		try {
			b = !rs.isLast();
		} catch (SQLException e) {
			// e.printStackTrace();
			dblogger.error("error when check the next SongPojo");
		}
		return b;
	}

	@Override
	public synchronized void remove() {
		// TODO Auto-generated method stub

	}

	/**
	 * write a record to updated the lyric and result state.
	 * 
	 * @param documentid
	 * @param lyric
	 */
	public synchronized void write(String documentid, ResultPojo result) {
		try {
			stmt_w.setString(1, result.getLyric());
			stmt_w.setInt(2, result.getCode());
			stmt_w.setString(3, documentid);
			stmt_w.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			dblogger.error("error when insert a lyric");
		}

	}

}
